import { _ as _export_sfc, x as useMainStore, y as storeToRefs, z as ref, U as onShow, V as getStorageSync, A as computed, g as createElementBlock, b as createVNode, w as withCtx, F as Fragment, i as index$g, j as resolveEasycom, k as resolveDynamicComponent, o as openBlock, s as index$i, t as createTextVNode, u as toDisplayString, P as ScrollView, c as createBlock, e as createCommentVNode, h as renderList, Q as index$x, a8 as showModal, a6 as setStorageSync, a1 as navigateTo, a5 as showLoading, a7 as hideLoading } from './index-Cc4oxzDN.js';
import { _ as __easycom_4 } from './uv-empty.ob-CT4XT.js';
import { _ as __easycom_4$1 } from './uv-button.CpuKY_t2.js';
import { b as __easycom_5 } from './uv-toast.nS_Liirv.js';
import './uv-loading-icon.DuNkB54M.js';

const _sfc_main = {
  __name: 'cart',
  setup(__props) {

const main = useMainStore();
const { orderType,address, store,location,isLogin } = storeToRefs(main);
ref('购物车');
const cart = ref([]);
const uToast = ref();

onShow(() => {
	//cart.value = []
	cart.value = getStorageSync('cart');
});
const getCartGoodsNumber = computed(() => { //计算购物车总数
	if(cart.value.length == 0) {
		return 0
	}
	return cart.value.reduce((acc, cur) => acc + cur.number, 0)
});
const getCartGoodsPrice = computed(() =>{ //计算购物车总价
	if(cart.value.length == 0) {
		return 0
	}
	let price = cart.value.reduce((acc, cur) => acc + cur.number * cur.price, 0);
	return parseFloat(price).toFixed(2);
});
const customStyle = computed(() =>{ 
	return {
		  paddingLeft:'60rpx',
		  paddingRight:'60rpx'
		}
});
const handleCartItemAdd = (index) => {
	cart.value[index].number += 1;
	setStorageSync('cart', JSON.parse(JSON.stringify(cart.value)));
};
const handleCartItemReduce = (index) => {
	if (cart.value[index].number === 1) {
		cart.value.splice(index, 1);
	} else {
		cart.value[index].number -= 1;
	}
	// if (!cart.value.length) {
	// 	cartPopupVisible.value = false
	// }
	setStorageSync('cart', JSON.parse(JSON.stringify(cart.value)));
};
const handleCartClear = () => { //清空购物车
	showModal({
		title: '提示',
		content: '确定清空购物车么',
		success: ({
			confirm
		}) => {
			if (confirm) {
				cart.value = [];
				setStorageSync('cart', JSON.parse(JSON.stringify(cart.value)));
			}
		}
	});
};
const toPay = () => {
	
	if(cart.value.length == 0){
		uToast.value.show({message:'请先去点餐哦',type: 'error'});
		return;
	}

	if (!isLogin.value) {
		navigateTo({
			url: '/pages/login/login'
		});
		return
	} else {
		if (store.value.status == 0) {
			uToast.value.show({message:'不在店铺营业时间内',type: 'error'});
			return;
		}
		// 判断当前是否在配送范围内
		if (orderType.value == 'takeout' && store.value.distance < store.value.far) {
			uToast.value.show({message:'选中的地址不在配送范围',type: 'error'});
			return;
		}

		showLoading({
			title: '加载中'
		});
		setStorageSync('cart', JSON.parse(JSON.stringify(cart.value)));

		navigateTo({
			url: '/pages/components/pages/pay/pay'
		});
	}

	hideLoading();
};

return (_ctx, _cache) => {
  const _component_v_uni_text = index$i;
  const _component_v_uni_view = index$g;
  const _component_uv_empty = resolveEasycom(resolveDynamicComponent("uv-empty"), __easycom_4);
  const _component_v_uni_button = index$x;
  const _component_v_uni_scroll_view = ScrollView;
  const _component_uv_button = resolveEasycom(resolveDynamicComponent("uv-button"), __easycom_4$1);
  const _component_uv_toast = resolveEasycom(resolveDynamicComponent("uv-toast"), __easycom_5);

  return (openBlock(), createElementBlock(Fragment, null, [
    createVNode(_component_v_uni_view, { class: "cart-popup" }, {
      default: withCtx(() => [
        createVNode(_component_v_uni_view, { class: "top flex justify-between" }, {
          default: withCtx(() => [
            createVNode(_component_v_uni_text, null, {
              default: withCtx(() => [
                createTextVNode("已点" + toDisplayString(getCartGoodsNumber.value) + "份", 1)
              ]),
              _: 1
            }),
            createVNode(_component_v_uni_text, { onClick: handleCartClear }, {
              default: withCtx(() => [
                createTextVNode("清空")
              ]),
              _: 1
            })
          ]),
          _: 1
        }),
        createVNode(_component_v_uni_scroll_view, {
          class: "cart-list",
          "scroll-y": ""
        }, {
          default: withCtx(() => [
            createVNode(_component_v_uni_view, { class: "wrapper" }, {
              default: withCtx(() => [
                (cart.value.length == 0)
                  ? (openBlock(), createBlock(_component_uv_empty, {
                      key: 0,
                      mode: "car"
                    }))
                  : createCommentVNode("", true),
                (openBlock(true), createElementBlock(Fragment, null, renderList(cart.value, (item, index) => {
                  return (openBlock(), createBlock(_component_v_uni_view, {
                    class: "item",
                    key: index
                  }, {
                    default: withCtx(() => [
                      createVNode(_component_v_uni_view, { class: "left" }, {
                        default: withCtx(() => [
                          createVNode(_component_v_uni_view, { class: "name" }, {
                            default: withCtx(() => [
                              createTextVNode(toDisplayString(item.name), 1)
                            ]),
                            _: 2
                          }, 1024),
                          createVNode(_component_v_uni_view, { class: "props" }, {
                            default: withCtx(() => [
                              createTextVNode(toDisplayString(item.valueStr), 1)
                            ]),
                            _: 2
                          }, 1024)
                        ]),
                        _: 2
                      }, 1024),
                      createVNode(_component_v_uni_view, { class: "center" }, {
                        default: withCtx(() => [
                          createVNode(_component_v_uni_text, null, {
                            default: withCtx(() => [
                              createTextVNode("￥" + toDisplayString(item.price), 1)
                            ]),
                            _: 2
                          }, 1024)
                        ]),
                        _: 2
                      }, 1024),
                      createVNode(_component_v_uni_view, { class: "right" }, {
                        default: withCtx(() => [
                          createVNode(_component_v_uni_button, {
                            type: "default",
                            plain: "",
                            size: "min",
                            class: "btn",
                            "hover-class": "none",
                            onClick: $event => (handleCartItemReduce(index))
                          }, {
                            default: withCtx(() => [
                              createVNode(_component_v_uni_view, { class: "iconfont iconsami-select" })
                            ]),
                            _: 2
                          }, 1032, ["onClick"]),
                          createVNode(_component_v_uni_view, { class: "number" }, {
                            default: withCtx(() => [
                              createTextVNode(toDisplayString(item.number), 1)
                            ]),
                            _: 2
                          }, 1024),
                          createVNode(_component_v_uni_button, {
                            type: "primary",
                            class: "btn",
                            size: "min",
                            "hover-class": "none",
                            onClick: $event => (handleCartItemAdd(index))
                          }, {
                            default: withCtx(() => [
                              createVNode(_component_v_uni_view, { class: "iconfont iconadd-select" })
                            ]),
                            _: 2
                          }, 1032, ["onClick"])
                        ]),
                        _: 2
                      }, 1024)
                    ]),
                    _: 2
                  }, 1024))
                }), 128))
              ]),
              _: 1
            })
          ]),
          _: 1
        })
      ]),
      _: 1
    }),
    createVNode(_component_v_uni_view, { class: "fixed-bottom flex justify-between align-center bg-white" }, {
      default: withCtx(() => [
        createVNode(_component_v_uni_view, { class: "mx-2 ont-weight-light" }, {
          default: withCtx(() => [
            createTextVNode("应付:"),
            createVNode(_component_v_uni_text, { class: "text-danger" }, {
              default: withCtx(() => [
                createTextVNode("￥" + toDisplayString(getCartGoodsPrice.value), 1)
              ]),
              _: 1
            })
          ]),
          _: 1
        }),
        createVNode(_component_v_uni_view, null, {
          default: withCtx(() => [
            createVNode(_component_uv_button, {
              type: "warning",
              color: "#dd524d",
              customStyle: customStyle.value,
              size: "large",
              text: "去结算",
              onClick: toPay
            }, null, 8, ["customStyle"])
          ]),
          _: 1
        })
      ]),
      _: 1
    }),
    createVNode(_component_uv_toast, {
      ref_key: "uToast",
      ref: uToast
    }, null, 512)
  ], 64))
}
}

};
const cart = /*#__PURE__*/_export_sfc(_sfc_main, [['__scopeId',"data-v-465d4d6e"]]);

export { cart as default };
